package com.abso.sunlight.explorer;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class CongressExplorerPlugin extends AbstractUIPlugin {

    /** The plug-in identifier. */
    public static final String PLUGIN_ID = "com.abso.sunlight.explorer";

    /** The SunlightLabs API key (registered for http://abso.freehostia.com). */
    public static final String API_KEY = "fcbb1f8f7fd38a4fd41aac14046ac78f";

    /** The plug-in's shared instance. */
    private static CongressExplorerPlugin plugin;

    /** The legislator's search manager. */
    private LegislatorSearchManager searchManager;

    @Override
    public void start(BundleContext context) throws Exception {
        super.start(context);
        plugin = this;
        searchManager = new LegislatorSearchManager();
    }

    @Override
    public void stop(BundleContext context) throws Exception {
        plugin = null;
        super.stop(context);
    }

    /**
     * Returns the shared instance
     * 
     * @return the shared instance
     */
    public static CongressExplorerPlugin getDefault() {
        return plugin;
    }

    /**
     * Returns the search manager.
     * 
     * @return the search manager.
     */
    public static LegislatorSearchManager getSearchManager() {
        return getDefault().searchManager;
    }

    /**
     * Returns the dialog settings for a specific section.
     * 
     * @param sectionName
     *            the section's name.
     * @return the dialog settings.
     */
    public IDialogSettings getDialogSettings(String sectionName) {
        IDialogSettings globalSettings = getDialogSettings();
        IDialogSettings settings = globalSettings.getSection(sectionName);
        if (settings == null) {
            settings = globalSettings.addNewSection(sectionName);
        }
        return settings;
    }

    /**
     * Returns an image descriptor for the image file at the given plug-in relative path
     * 
     * @param path
     *            the path
     * @return the image descriptor
     */
    public static ImageDescriptor getImageDescriptor(String path) {
        return imageDescriptorFromPlugin(PLUGIN_ID, path);
    }

    @Override
    protected void initializeImageRegistry(ImageRegistry reg) {
        for (SunlightImages image : SunlightImages.values()) {
            reg.put(image.toString(), imageDescriptorFromPlugin(PLUGIN_ID, image.getPath()));
        }
    }

    /**
     * Shows a message dialog.
     * 
     * @param shell
     *            the parent shell of the dialog.
     * @param dialogTitle
     *            the title to use for this dialog.
     * @param dialogMessage
     *            the message to show in this dialog.
     * @param statusMessage
     *            the error status message.
     * @param t
     *            the exception to be shown.
     */
    public void showErrorDialog(final Shell shell, final String dialogTitle, final String dialogMessage, final String statusMessage,
            final Throwable t) {
        runUIAsync(shell, new Runnable() {
            public void run() {
                ErrorDialog.openError(shell, dialogTitle, dialogMessage, new Status(IStatus.ERROR, PLUGIN_ID, 0, statusMessage, t));
            }
        });
    }

    /**
     * Runs a runnable in the user-interface thread.
     * 
     * @param shell
     *            the shell.
     * @param runnable
     *            the runnable being run.
     */
    public void runUIAsync(Shell shell, Runnable runnable) {
        Display display = shell.getDisplay();
        if ((display != null) && !display.isDisposed()) {
            display.asyncExec(runnable);
        }
    }

}
